home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / sox / sndrtool.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  161 lines

  1. /*
  2.  * Sounder/Sndtool format handler: W V Neisius, February 1992
  3.  */
  4.  
  5. #include <math.h>
  6. #include "st.h"
  7. #ifdef    VMS
  8. #include <errno.h>
  9. #include <perror.h>
  10. #endif
  11.  
  12. /* Private data used by writer */
  13. struct sndpriv {
  14.         unsigned long nsamples;
  15. };
  16.  
  17. #ifndef    SEEK_CUR
  18. #define    SEEK_CUR    1
  19. #endif
  20.  
  21. IMPORT sndtwriteheader(P2(ft_t ft,long nsamples));
  22.  
  23. /*======================================================================*/
  24. /*                         SNDSTARTREAD                                */
  25. /*======================================================================*/
  26.  
  27. sndtstartread(ft)
  28. ft_t ft;
  29. {
  30. struct sndpriv *p = (struct sndpriv *) ft->priv;
  31.  
  32. char buf[97];
  33.  
  34. long rate;
  35.  
  36. rate = 0;
  37.  
  38. /* determine file type */
  39.         /* if first 5 bytes == SOUND then this is probably a sndtool sound */
  40.         /* if first word (16 bits) == 0 
  41.          and second word is between 4000 & 25000 then this is sounder sound */
  42.         /* otherwise, its probably raw, not handled here */
  43.  
  44. if (fread(buf, 1, 2, ft->fp) != 2)
  45.     fail("SND: unexpected EOF");
  46. if (strncmp(buf,"\0\0",2) == 0)
  47.     {
  48.     /* sounder */
  49.     rate = rlshort(ft);
  50.     if (rate < 4000 || rate > 25000 )
  51.         fail ("SND: sample rate out of range");
  52.     fseek(ft->fp,4,SEEK_CUR);
  53.     }
  54. else
  55.     {
  56.     /* sndtool ? */
  57.     fread(&buf[2],1,6,ft->fp);
  58.     if (strncmp(buf,"SOUND",5))
  59.         fail ("SND: unrecognized SND format");
  60.     fseek(ft->fp,12,SEEK_CUR);
  61.     rate = rlshort(ft);
  62.     fseek(ft->fp,6,SEEK_CUR);
  63.     if (fread(buf,1,96,ft->fp) != 96)
  64.         fail ("SND: unexpected EOF in SND header");
  65.     report ("%s",buf);
  66.     }
  67.  
  68. ft->info.channels = 1;
  69. ft->info.rate = rate;
  70. ft->info.style = UNSIGNED;
  71. ft->info.size = BYTE;
  72.  
  73. }
  74.  
  75. /*======================================================================*/
  76. /*                         SNDTSTARTWRITE                               */
  77. /*======================================================================*/
  78. sndtstartwrite(ft)
  79. ft_t ft;
  80. {
  81. struct sndpriv *p = (struct sndpriv *) ft->priv;
  82.  
  83. /* write header */
  84. ft->info.style = UNSIGNED;
  85. ft->info.size = BYTE;
  86. p->nsamples = 0;
  87. sndtwriteheader(ft, 0);
  88.  
  89. }
  90. /*======================================================================*/
  91. /*                         SNDRSTARTWRITE                               */
  92. /*======================================================================*/
  93. sndrstartwrite(ft)
  94. ft_t ft;
  95. {
  96. /* write header */
  97. ft->info.style = UNSIGNED;
  98. ft->info.size = BYTE;
  99.  
  100. /* sounder header */
  101. wlshort (ft,0); /* sample size code */
  102. wlshort (ft,(int) ft->info.rate);     /* sample rate */
  103. wlshort (ft,10);        /* volume */
  104. wlshort (ft,4); /* shift */
  105. }
  106.  
  107. /*======================================================================*/
  108. /*                         SNDTWRITE                                     */
  109. /*======================================================================*/
  110.  
  111. sndtwrite(ft, buf, len)
  112. ft_t ft;
  113. long *buf, len;
  114. {
  115.     struct sndpriv *p = (struct sndpriv *) ft->priv;
  116.     p->nsamples += len;
  117.     rawwrite(ft, buf, len);
  118. }
  119.  
  120. /*======================================================================*/
  121. /*                         SNDTSTOPWRITE                                */
  122. /*======================================================================*/
  123.  
  124. sndtstopwrite(ft)
  125. ft_t ft;
  126. {
  127. struct sndpriv *p = (struct sndpriv *) ft->priv;
  128.  
  129. /* fixup file sizes in header */
  130. if (fseek(ft->fp, 0L, 0) != 0)
  131.     fail("can't rewind output file to rewrite SND header");
  132. sndtwriteheader(ft, p->nsamples);
  133. }
  134.  
  135. /*======================================================================*/
  136. /*                         SNDTWRITEHEADER                              */
  137. /*======================================================================*/
  138. sndtwriteheader(ft,nsamples)
  139. ft_t ft;
  140. long nsamples;
  141. {
  142. char name_buf[97];
  143.  
  144. /* sndtool header */
  145. fputs ("SOUND",ft->fp); /* magic */
  146. fputc (0x1a,ft->fp);
  147. wlshort (ft,(long)0);  /* hGSound */
  148. wllong (ft,nsamples);
  149. wllong (ft,(long)0);
  150. wllong (ft,nsamples);
  151. wlshort (ft,(int) ft->info.rate);
  152. wlshort (ft,0);
  153. wlshort (ft,10);
  154. wlshort (ft,4);
  155. sprintf (name_buf,"%s - File created by Sound Exchange",ft->filename);
  156. fwrite (name_buf, 1, 96, ft->fp);
  157.  
  158. }
  159.  
  160.  
  161.